package com.imooc.ecommerce.conf;

import com.alibaba.cloud.seata.web.SeataHandlerInterceptor;
import com.imooc.ecommerce.filter.LoginUserInfoInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * web mvc配置
 * @Author: zhangshao
 * @Since: 2023/11/27 17:28
 */
@Configuration
public class ImoocWebMvcConfig extends WebMvcConfigurationSupport {

	/**
	 * 添加拦截器配置
	 * @param registry
	 */
	@Override
	protected void addInterceptors(InterceptorRegistry registry) {
		// 添加用户身份统一登录拦截的拦截器
		registry.addInterceptor(new LoginUserInfoInterceptor())
				.addPathPatterns("/**").order(0);

		//Seata传递xid 事务id 给其他的微服务
		//只有这样,其他的服务才会些undo_log, 才能够实现回滚
		registry.addInterceptor(new SeataHandlerInterceptor()).addPathPatterns("/**");
	}

	/**
	 * 让 MVC 加载swagger 静态资源
	 * @param registry
	 */
	/**
	 * <h2>让 MVC 加载 Swagger 的静态资源</h2>
	 * */
	@Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {

		registry.addResourceHandler("/**").
				addResourceLocations("classpath:/static/");
		registry.addResourceHandler("swagger-ui.html")
				.addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("doc.html")
				.addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**")
				.addResourceLocations("classpath:/META-INF/resources/webjars/");

		super.addResourceHandlers(registry);
	}
}

